<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>货件预览</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<!-- jQuery-->
		<script type="text/javascript" src="/static/plugins/jquery/jquery-3.6.0.min.js"></script>

		<!-- layui-->
		<link rel="stylesheet" href="/static/plugins/layui/2.7.6/css/layui.css" media="all">
		<script src="/static/plugins/layui/2.7.6/layui.js" charset="utf-8"></script>

		<!-- common -->
		<link rel="stylesheet" href="/static/admin/css/common.css" media="all" />
		<link rel="stylesheet" href="/static/admin/css/mini-ease.css" media="all" />

		<!--样式-->
		<th:block th:include="include/src :: form_base_js_and_css"/>

	</head>

	<body>

	<form class="layui-form">
		<!-- 列表 -->
		<div class="layui-row layui-col-space5" style="width:90%;margin: 0 auto;">

			<div class="layui-col-md12" id="id_panel_left_grid">
				<div class="layui-panel" style="overflow: auto; padding-top: 0.2rem;">
					<div class="mid-content">
						<table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
					</div>
				</div>
			</div>
		</div>


		<script type="text/html" id="toolEventDemo">
			<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="detail">查看详情</a>
		</script>

		<footer>
			<div class="layer-footer" style="position: fixed; bottom: 0;width:100%;height:50px" id="chooseBtn" >
				<div class="layui-layer-btn layui-layer-btn-"><a class="layui-layer-btn0" onclick="confirm()" id="chooseBtnTxt">确认</a><a class="layui-layer-btn1" onclick="cancel()">取消</a></div>
			</div>
		</footer>
	</form>

  </body>



<script>
	var dataPost = null;
	var bidPost = null;

	var tableId = '[[${tableId}]]';


	var isEdit = '[[${isEdit}]]';////是否可编辑 0否1是
	var choosePlacementOptionId = '[[${choosePlacementOptionId}]]';//选择的方案id

	var apiCount = '[[${apiCount}]]';

	var isNeedToApi = '[[${isNeedToApi}]]';
    var isNeedToWait = '[[${isNeedToWait}]]';
	var btnTxt = '确认';

	var btnTip = '确认是否选择该货件方案吗？';

	if(apiCount==0){
		btnTxt = '确认';
		btnTip = '确认是否选择该货件方案吗？';
	}else if(apiCount==1){
		btnTxt = '上传方案';
		btnTip = '确认是否重新上传已选择的方案吗？';
	}else if(apiCount==2){
		btnTxt = '重新请求并获取运输选项与交付窗口';
		btnTip = '确认是否重新请求并获取运输选项与交付窗口？';
	}else if(apiCount==3){
		btnTxt = '重新设置交付窗口';
		btnTip = '确认是否重新设置交付窗口？';
	}else if(apiCount==4){
		btnTxt = '重新设置运输选项';
		btnTip = '确认是否重新设置运输选项？';
	}else{
		$('#chooseBtn').hide();
	}
	$('#chooseBtnTxt').text(btnTxt);
	var dataList = [];

    // if(isNeedToWait==1){
    //     //parent.window.layer.closeAll();
    //     parent.window.layer.alert("系统正在获取方案中，请稍后");
    //     isEdit = 0;
    // }

	layui.config({
		//自定义layui组件的目录,设定扩展的layui模块的所在目录，一般用于外部模块扩展
		base: '/static/js/layui_extends/'
	}).use(['form', 'element', 'table', 'dropdown', 'util', 'laydate', 'upload', 'YsModal', 'jquery'], function(){
		var table = layui.table;
		var $ = layui.jquery;

		var bid = '[[${bid}]]';

        if(isNeedToWait==1){
			setTimeout(function(){
				parent.window.layer.closeAll();
				parent.window.layer.alert("系统正在获取方案中，请稍后");
			}, 1000);
			return;
        }
		if(isNeedToApi==1){
			layer.alert("获取方案可能需要等待几分钟时间，请耐心等待");
		}

		//获取列表数据
		$.ajax({
			url : '/api/inter/tccFbaBoxUp/getShipmentList',
			data : {bid:bid},
			type : 'POST',
			async : false,//关闭异步
			success : function(result) {
				if (result.code == '0') {
					var resList = result.data;
					if(isEdit== '0'){
						dataList = [];
						var isSetChoose = false;
						for(var i=0;i<resList.length;i++){
							var row = resList[i];
							row['LAY_DISABLED'] = true;
							if(resList[i].placementOptionId == choosePlacementOptionId && !isSetChoose){
								dataPost = resList[i];
								row['LAY_CHECKED'] = true;
								isSetChoose = true;
							}else{
								row['LAY_CHECKED'] = false;
							}
							dataList.push(row);
						}
					}else {
						dataList = result.data;
					}
					bidPost = bid;
				}else{
					layer.alert(result.msg);
				}
			}
		});

		//console.log('dataList',dataList);

		var cols = [
			{title: '', type: 'radio',field:'radioNum',  width: '40'},
			{title: '货件方案',   width: '120' , field:'shippingSolution' ,align:"center" },
			{title: '费用',   width: '120' , field:'fee' ,align:"center" },
			{title: '货件名称', width: '160' , field:'fbaName', align:"center" , hide: true},
			{title: '放置id', width: '160' , field:'placementOptionId', align:"center" , hide: true},
			{title: '货件id', width: '160' , field:'shipmentId', align:"center" },
			{title: '仓库标识', width: '120' , field:'warehouseId', align:"center" },
			{title: '操作', width: 155, templet: '#toolEventDemo',align:"center",field:'toolEvent'}
		];

		// 初始化表格
		table.render({
			elem: '#dhtable'
			,data: dataList
			, page: false
			, even: false
			, limit: 10000
			, totalRow: []
			, cols: [cols]
			// , height: '400'
			// , width: '1500'
			, done: function (res, curr, count) {
				//合并行
				mergeRows(res)
			},success:function(a,b,c){
				console.log(a,b,c)
			}
		});

		//单元格工具事件
		table.on('tool(ftable)', function(obj){ // 注：test 是 table 原始标签的属性 lay-filter="对应的值"
			var data = obj.data; //获得当前行数据
			var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
			var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

			if(layEvent === 'detail'){ //查看
				var w = ($(window).width() * 0.9);
				var h = ($(window).height() * 0.8);

				layer.open({
					type: 2, area: [w + 'px', h + 'px'],
					fix: true,
					maxmin: false,
					shadeClose: true,
					shade: 0.4,
					title: "货件详情",
					content: '/web/storage/tccFbaBoxUp/shipmentDetail?bid=' + bid + "&shipmentId=" + data.shipmentId,
				});
			}

		});

		//单选框选择数据
		table.on('radio(ftable)', function(obj){
			dataPost = obj.data;
		});

		//合并行
		function mergeRows(res) {
			var data = res.data;
			var mergeIndex = 0;//定位需要添加合并属性的行数
			var mark = 1; //这里涉及到简单的运算，mark是计算每次需要合并的格子数
			var columsName = ["radioNum","shippingSolution","fee"];//需要合并的列名称
			var columsIndex = [0,1,2];//需要合并的列索引值
			for (var k = 0; k < columsName.length; k++) { //这里循环所有要合并的列
				var trArr = $(".layui-table-main>.layui-table").find("tr");
				for (var i = 1; i < res.data.length; i++) { //这里循环表格当前的数据
					var tdCurArr = trArr.eq(i).find("td").eq(columsIndex[k]);//获取当前行的当前列
					var tdPreArr = trArr.eq(mergeIndex).find("td").eq(columsIndex[k]);//获取相同列的第一列

					if (data[i]['radioNum'] === data[i - 1]['radioNum']) { //后一行的值与前一行的序号值做比较，相同就需要合并
						mark += 1;
						tdPreArr.each(function () {//相同列的第一列增加rowspan属性
							$(this).attr("rowspan", mark);
						});
						tdCurArr.each(function () {//当前行隐藏
							$(this).css("display", "none");
						});
					} else {
						mergeIndex = i;
						mark = 1;//一旦前后两行的值不一样了，那么需要合并的格子数mark就需要重新计算
					}
				}
				mergeIndex = 0;
				mark = 1;
			}
		}
	});

	//确认
	function confirm() {
		if (dataPost == null) return layer.msg("请选择货件方案!");
		layer.confirm(btnTip, { icon: 3 }, function() {
			layer.load(0);
			if(isEdit== '1'){
				var isSetChoose = false;
				//将货件选择框禁用
				dataList = [];
				for(var i=0;i<dataList.length;i++){
					var row = dataList[i];
					row['LAY_DISABLED'] = true;
					if(row.placementOptionId == dataPost.placementOptionId && !isSetChoose){
						row['LAY_CHECKED'] = true;
						isSetChoose = true;
					}else{
						row['LAY_CHECKED'] = false;
					}
					dataList.push(row);
				}
				layui.table.reloadData("dhtable");
				isEdit= '0';
			}

			$.ajax({
				url: '/api/inter/tccFbaBoxUp/yesChooseIdea',
				data: {
					dataPost: JSON.stringify(dataPost),
					bid:bidPost
				},
				type: 'POST',
				async: true,
				success: function(res) {
					if (res.code == 0) {
						parent.window.layer.closeAll();
						parent.window.layui.table.reloadData(tableId);
						layer.alert(res.msg);
					} else {
						//parent.window.layer.closeAll();
						layer.alert(res.msg);
					}
				}, error: function() {
					layer.closeAll();
					layer.alert("操作失败，请稍后再试！", { icon: 2 });
				}
			});
		}, function() {
		});
	}

	//取消
	function cancel() {
		var index = parent.layer.getFrameIndex(window.name);
		parent.layer.close(index);
	}
</script>
</html>